﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WLMCoreLibrary;
using MiWebSite_Core;

namespace MWS_AddDirectory.MisAnuncios
{
    public partial class OlvidePassword : System.Web.UI.Page
    {
        public dbConnector ConexionDB;
        public WebSiteMainConfiguration config;
        protected void Page_Load(object sender, EventArgs e)
        {
            config = WebSiteMainConfiguration.GetConfig();
            if (!IsPostBack)
            {
                lblDirectorio.Text = DeCurrentDomain.DomainName;
                lblDirectorio2.Text = DeCurrentDomain.DomainName;
                lblDirectorio3.Text = DeCurrentDomain.DomainName;
            }
            if (Request.QueryString["recover"] != null)
                guardarDatosUsuario();
        }

        protected void guardarDatosUsuario()
        {

            ConexionDB = new dbConnector(config.Connections["WebSite"].value);
            Response.ContentType = "text/xml";
            try
            {
                Cliente cliente = new Cliente(username.Text, ref ConexionDB);
                if (cliente.id != 0)
                {
                    SaltedHash hash = new SaltedHash();
                    string data = WLMFunctions.CreateRandomPassword(8);
                    string newHash = hash.GetStringHash(data, cliente.salt);

                    cliente.password = newHash;
                    cliente.Save();
                    cliente.DatoTemporal = data;
                    cliente.EnviarEmailRecuperacionPassword(data);
                    Response.Write(WLMFunctions.SerializeObject(
                        new SuccessObject(SuccessCode.RegistryUpdated,"Password Regenerado")
                        ,true));
                }
            }
            catch (Exception ex)
            {
                Response.Write(WLMFunctions.SerializeObject(
                       new ErrorObject(ErrorCode.CompilationError, ex.Message, ex.Message, ex.StackTrace)
                       , true));
            }
            ConexionDB.CloseConnection();
            Response.End();
        }
    }
}